package com.ad.backstage.controller;

import com.ad.backstage.common.utils.BeanUtil;
import com.ad.backstage.model.UploadRecord;
import com.ad.backstage.service.UploadService;
import com.ad.payload.dto.UploadRecordEditDto;
import com.ad.payload.util.StringUtil;
import com.ad.payload.vo.UploadFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author:dxgong
 * @日期 2021/4/24 09:45
 * @注释
 */
@RequestMapping("UploadRecordController")
@RestController
public class UploadRecordController {


    @Autowired
    private UploadService uploadService;


    /**
     *  cos上传图片
     *
     *  @param file
     *  @param bucketName cos 桶名
     * */
    @RequestMapping("/back/auth/uploadFile")
    public UploadFileVo uploadFile(@RequestParam(value="file",required=false) MultipartFile file, String bucketName, String businessType){
        if(StringUtil.isBlank(bucketName)){
            throw new RuntimeException("路径不能为空!");
        }
        return uploadService.uploadFileForBusinessType(file, bucketName, businessType);
    }



    /**
     * 修改上传记录
     *
     * @param uploadRecordEditDto 上传记录
     * */
    @RequestMapping("/back/auth/updateUploadRecord")
    public Long updateUploadRecord(@RequestBody @Valid UploadRecordEditDto uploadRecordEditDto){
        UploadRecord uploadRecord = new UploadRecord();
        BeanUtil.copyProperties(uploadRecordEditDto, uploadRecord);
        return uploadService.updateUploadRecord(uploadRecord);
    }



}
